import{
  ADD_COUNTER,
  ADD_TO_CART
} from'./mutations-types.js'
import {Toast} from 'vant'
export default{
     [ADD_COUNTER](state,payload){
     payload.count++
     Toast({
       type:'success',
       message:`当前商品数量为${payload.count}`,
       // 禁止点击
       forbidClick:true,
       duration:1500
     })
     localStorage.setItem("cartList",JSON.stringify(state.cartList))
    },
    [ADD_TO_CART](state,payload){
      payload.checked = true
      Toast({
        type:'success',
        message:'加入购物车成功!',
        forbidClick:true,
        duration:1500
      })
      state.cartList.push(payload)
      localStorage.setItem("cartList",JSON.stringify(state.cartList))
    },
    clearCartList(state){
      let result = state.cartList.filter(item=>item.checked !== true)
      if(result.length ===0){
        localStorage.removeItem("cartList")
        state.cartList =[]
      }else{
        state.cartList = result
        localStorage.setItem("cartList",JSON.stringify(state.cartList))
      }
    },
    setCartList(state,payload){
      state.cartList = payload
    },
    setLoading(state,payload){
      state.isLoading = payload
    }
}
